package com.custom.base.entity

import com.google.gson.Gson
import com.custom.base.config.BaseConfig
import com.custom.base.http.BaseHttpModel
import com.google.gson.GsonBuilder


import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.*

/**
 * 作者：
 * 日期：2019/8/8
 * 描述：
 */
class OkHttpBodyEntity(var baseModel: BaseHttpModel) {
    private var url: String = ""
    private var api: String = ""
    private val data: HashMap<String, Any> = HashMap()
    private var arr: Any? = null
    private var isArr = false
    private var method = METHOD.POST
    private var type = BaseConfig.getInstance.getFromDataType()

    val formBodyBu: RequestBody?
        get() {
            if(method == METHOD.POST){
                val gson= GsonBuilder().disableHtmlEscaping().create()
                baseModel.http = getUrl() + "/" + api + " " + if (method == METHOD.POST) (if(isArr) gson.toJson( arr ) else gson.toJson( data )) else ""
            }
            if (type == TYPE.FORM_DATA && method == METHOD.POST) {
                val sb = StringBuffer()
                for (s in data.keys) {
                    sb.append(s + "=" + (if (data[s] == null) "" else data[s]!!.toString()) + "&")
                }
                return RequestBody.create(
                    "application/x-www-form-urlencoded; charset=utf-8".toMediaTypeOrNull(),
                    sb.toString()
                )
            }else if (type == TYPE.RAW && method == METHOD.POST) {
                return RequestBody.create(
                    "application/json;charset=UTF-8".toMediaTypeOrNull(),
                    Gson().toJson( if(isArr) arr else data )
                )
            }
            return null
        }

    fun setPost(a: String): OkHttpBodyEntity {
        api = a
        method = METHOD.POST
        return this
    }

    fun setGet(a: String = ""): OkHttpBodyEntity {
        api = a
        method = METHOD.GET
        return this
    }

    /**
     * 设置URL
     */
    fun setUrl(u: String = ""): OkHttpBodyEntity {
        this.url = u
        return this
    }

    /**
     * 获取URL
     */
    fun getUrl():String{
        return if(url!="") url else BaseConfig.getInstance.getUrl()
    }

    /**
     * 设置请求参数类型为 form-data
     */
    fun setTypeFormData(){
        type = TYPE.FORM_DATA
    }
    /**
     * 设置请求参数类型为 raw
     */
    fun setTypeRaw(){
        type = TYPE.RAW
    }

    fun add(key: String, value: Any): OkHttpBodyEntity {
        isArr = false
        data[key] = value
        return this
    }
    fun <T> addArr(value: MutableList<T>): OkHttpBodyEntity {
        isArr = true
        arr = value
        return this
    }
    /**
     * 加入分页
     * @param model
     */
    fun addPage(model: PageModel): OkHttpBodyEntity {
        data[BaseConfig.getInstance.getPageName()] = model.page
        data[BaseConfig.getInstance.getPageSizeName()] = model.pageSize
        return this
    }

    /**
     * 获取API
     */
    fun getApi(): String {
        if (method == METHOD.GET) {
            api += "?"
            for (key in data.keys) {
                api += key + "=" + data[key] + "&"
            }
            api = api.substring(0, api.length - 1)
            baseModel.http = getUrl() + "/" + api
        }
        return api
    }

    /**
     * 请求类型
     */
    enum class METHOD{
        POST,
        GET
    }
    /**
     * 请求参数类型
     */
    enum class TYPE{
        RAW,
        FORM_DATA,
    }
}
